package com.ledim.chat.model;

import android.text.TextUtils;

import com.ledim.chat.ConnectionManager;
import com.ledim.chat.ByteArrayBuilder;
import com.ledim.chat.ByteArrayReader;
import com.ledim.utils.LogUtil;
import com.letv.core.bean.LetvBaseBean;

import java.io.UnsupportedEncodingException;

public class BaseSocketMessage implements LetvBaseBean, ISocketMessage {

    /** 消息头 **/
    public MessageHeader header;
    /** 消息body **/
    public String body;

    @Override
    public BaseSocketMessage parse(byte[] bytes) {
        if (bytes.length >= MessageHeader.LENGTH) {
            ByteArrayReader reader = new ByteArrayReader(bytes);
            header = new MessageHeader();
            header.cmd = reader.readUint16();
            header.flag = reader.readUint16();
            header.len = reader.readUint32();
            header.from = reader.readUint32();
            header.to = reader.readUint32();
            header.sequence = reader.readUint32();
            header.mid = reader.readUint16();
            header.version = reader.readUint16();

            //body
            if (header.len > 0) {
                body = reader.readString(header.len);
            }
        }
        return this;

    }

    /**
     * 解析header，并返回相应Message
     *
     * @param bytes
     * @return
     */
    public static BaseSocketMessage getMsgByHeadData(byte[] bytes) {
        if (bytes != null && bytes.length >= MessageHeader.LENGTH) {
            ByteArrayReader reader = new ByteArrayReader(bytes);
            MessageHeader head = new MessageHeader();
            head.cmd = reader.readUint16();
            head.flag = reader.readUint16();
            head.len = reader.readUint32();
            head.from = reader.readUint32();
            head.to = reader.readUint32();
            head.sequence = reader.readUint32();
            head.mid = reader.readUint16();
            head.version = reader.readUint16();

            BaseSocketMessage bm = new BaseSocketMessage();
            bm.header = head;
            return bm;
        }
        return new BaseSocketMessage();
    }

    @Override
    public ISocketMessage parseHeader(MessageHeader header) {
        this.header = header;
        return this;
    }

    @Override
    public ISocketMessage parseBody(byte[] bytes) {
        //body
        if (header != null && bytes != null && header.len > 0) {
            ByteArrayReader reader = new ByteArrayReader(bytes);
            body = reader.readString(header.len);
            LogUtil.e("ledimchat", "parseBody body=" + body);
        }
        return this;
    }

    /**
     * 生成结构消息数据
     *
     * @param cmd
     * @param flag
     * @param from
     * @param to
     * @param sequence
     * @param mid
     * @param version
     * @param body
     */
    public static byte[] getStructMessage(int cmd, int flag, int from, int to, int sequence,
                                          int mid, int version, String body) {
        int len = MessageHeader.LENGTH;
        int bodyLen = 0;
        byte[] bodyBt = null;
        if (!TextUtils.isEmpty(body)) {
            try {
                bodyBt = body.getBytes("UTF8");
                bodyLen = bodyBt.length;
                len += bodyLen;
                LogUtil.e(ConnectionManager.TAG, "getBytes..bodyLen=" + bodyLen + ",len=" + len);
            } catch (UnsupportedEncodingException e) {
            }
        }
        return new ByteArrayBuilder(len).writeUint16(cmd)
                                        .writeUint16(flag)
                                        .writeUint32(bodyLen)
                                        .writeUint32(from)
                                        .writeUint32(to)
                                        .writeUint32(sequence)
                                        .writeUint16(mid)
                                        .writeUint16(version)
                                        .write(bodyBt)
                                        .toBytes();
    }

    @Override
    public int getMessageLength() {
        int length = 0;
        if (header != null) {
            length = MessageHeader.LENGTH + header.len;
        }
        return length;
    }

    @Override
    public byte[] getBytes() {
        return new byte[0];
    }

    @Override
    public LetvBaseBean getBodyBean() {
        return null;
    }
}
